import {createRouter,createWebHashHistory} from 'vue-router'

//公共路由
const constantRoutes = [
    {
        path: '/',
        name: 'Item',
        component: () => import('@/components/Item')
    },
    {
        path: '/about',
        name: 'About',
        component:() => import('@/components/About'),
        children:[//二级路由 二级路由不加斜杠
            {
                path:"aboutus",
                component:() => import('@/components/About/AboutUs')
            },
            {
                path:"aboutyour",
                component:() => import('@/components/About/AboutYour')
            },
        ]
    },
    //登录路由
    {
        path:'/login',
        name:'Login',
        component:() => import('@/components/Login')
    },
    //404用
    {
        path:'/:pathMatch(.*)*',
        name:'NotFound',
        component:() => import('@/components/error/404')
    }
]

//动态路由

//实例化路由
const router = createRouter({
    // 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
    history: createWebHashHistory(),
    routes:constantRoutes, // `routes: routes` 的缩写
})

//全局前置守卫
router.beforeEach((to, from, next) => {
    console.log("to",to);
    console.log("from",from);
    //判断是否是登录页面
    if(to.name == 'Login'){
        next();
    }else{
        //登录验证
        next({name:'Login'});
    }
    // ...
    // 返回 false 以取消导航
    return true
})

export default router